﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConvertServerNet
{
    public class CarInfo
    {
        public string LocationNo;
        public string DirectionNo;
        public int LaneNo;
        public string Ip;
        public int Channel;
        public int LocationType; //监控点类型
        public int CaptureMode; //抓拍模式
        public int GroupNo;
        public DateTime CaptureTime;
        public int SequenceNo; //序列号
    }


    public class ConvertWorker
    {
        public static string IpPrefix = "192.168";

        public static IEnumerable<Tuple<CarInfo, string>[]> ParseFiles(IEnumerable<string> fullPaths)
        {
            var parsedItem = from path in fullPaths
                             let filename = Path.GetFileNameWithoutExtension(path)
                             let carinfo = ParseFileName(filename)
                             where carinfo != null
                             orderby carinfo.CaptureTime ascending
                             select new Tuple<CarInfo, string>(carinfo, path);
            var parsedGroup =
                parsedItem.GroupBy(
                    t =>
                        string.Format("{0}-{1}-{2}-{3}", 
                        t.Item1.LocationNo, t.Item1.DirectionNo, 
                        t.Item1.LaneNo, t.Item1.GroupNo));

            foreach (var g in parsedGroup)
            {
                var lastTime = DateTime.MinValue;
                var groupWithSameTime = new List<Tuple<CarInfo, string>>();
                foreach (var item in g)
                {
                    if (item.Item1.CaptureTime - lastTime > TimeSpan.FromMinutes(5))
                    {
                        if (groupWithSameTime.Count > 0)
                        {
                            yield return groupWithSameTime.ToArray();
                        }
                        groupWithSameTime = new List<Tuple<CarInfo, string>>();
                    }
                    
                    groupWithSameTime.Add(item);
                    lastTime = item.Item1.CaptureTime;
                }

                if (groupWithSameTime.Count > 0)
                {
                    yield return groupWithSameTime.ToArray();
                }
            }
        }

        public static CarInfo ParseFileName(string value)
        {
            var sections = value.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
            if (sections.Length != 11)
            {
                return null;
            }

            try
            {
                var locationNo = sections[0];
                var directionNo = sections[1];
                var laneNo = int.Parse(sections[2]);
                var ip3 = int.Parse(sections[3]);
                var ip4 = int.Parse(sections[4]);
                var channel = int.Parse(sections[5]);
                var locationType = int.Parse(sections[6]);
                var captureMode = int.Parse(sections[7]);
                var groupNo = int.Parse(sections[8]);
                var captureTime = DateTime.ParseExact(sections[9], "yyMMddHHmmss", CultureInfo.InvariantCulture);
                var seqNo = int.Parse(sections[10]);

                var result = new CarInfo();
                result.LocationNo = locationNo;
                result.DirectionNo = directionNo;
                result.LaneNo = laneNo;
                result.Ip = string.Format("{0}.{1}.{2}", IpPrefix, ip3, ip4);
                result.Channel = channel;
                result.LocationType = locationType;
                result.CaptureMode = captureMode;
                result.GroupNo = groupNo;
                result.CaptureTime = captureTime;
                result.SequenceNo = seqNo;

                return result;
            }
            catch (Exception ex)
            {
                return null;
            }

        }
    }
}
